package com.example.controller;

import com.example.domain.Orders;
import com.example.domain.OrdersCustom;
import com.example.domain.User;
import com.example.service.OrdersService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;


    //延迟加载
    @RequestMapping("/findOrdersUserLazyLoading")
    public List<Orders> findOrdersUserLazyLoading() throws JsonProcessingException {
        return ordersService.findOrdersUserLazyLoading();
    }

    //多对多查询
    @RequestMapping("/findUserAndItemsResultMap")
    public List<User> findUserAndItemsResultMap() throws JsonProcessingException {
        return ordersService.findUserAndItemsResultMap();
    }

    //一对多查询
    @RequestMapping("/findOrdersAndOrderDetailResultMap")
    public List<Orders> findOrdersAndOrderDetailResultMap() throws JsonProcessingException {
        return ordersService.findOrdersAndOrderDetailResultMap();
    }

    //一对一查询(resultMap，可实现延迟加载)
    @RequestMapping("/findOrdersUserResultMap")
    public List<Orders> findOrdersUserResultMap() throws JsonProcessingException {
        return ordersService.findOrdersUserResultMap();
    }

    //一对一查询(resultType)
    @RequestMapping("/findOrdersUser")
    public List<OrdersCustom> findOrdersUser() throws JsonProcessingException {
        return ordersService.findOrdersUser();
    }

    //CRUD
        @RequestMapping("/findAll")
        public List<Orders> findAll() throws JsonProcessingException {
             return ordersService.findAll();
        }

    //根据id查询
    @RequestMapping("/findById/{id}")
    public Orders findById(@PathVariable("id") Integer id) {
        return ordersService.findById(id);
    }

    //新增
    @RequestMapping("/save")
    public void save(Orders computers) {
        ordersService.save(computers);
    }

    //修改
    @PutMapping("/update/{id}")
    public void update(@PathVariable("id") Integer id,Orders computers) {
        ordersService.update(computers);
    }

    //删除
    @RequestMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        ordersService.delete(id);
    }
}
